package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.dto.ActivityAuditDTO;
import com.feiyisys.dto.ActivityRegistrationDTO;
import com.feiyisys.dto.ActivityRegistrationResponseDTO;
import com.feiyisys.service.ActivityRegistrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

/**
 * <p>
 * 活动报名表 前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/activityRegistration")
@Tag(name = "活动报名管理")
public class ActivityRegistrationController {

    @Resource
    private ActivityRegistrationService activityRegistrationService;

    /**
     * 用户报名活动
     * @param registrationDTO 报名信息
     * @param userId 用户ID
     * @return 报名结果
     */
    @Operation(summary = "用户报名活动")
    @PostMapping("/register")
    public Result<ActivityRegistrationResponseDTO> registerActivity(
            @RequestBody ActivityRegistrationDTO registrationDTO,
            @RequestHeader("userId") Long userId) {
        try {
            ActivityRegistrationResponseDTO result = activityRegistrationService.registerActivity(userId, registrationDTO);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.fail("报名失败，请稍后重试");
        }
    }

    /**
     * 管理员审核活动报名
     * @param auditDTO 审核信息
     * @return 审核结果
     */
    @Operation(summary = "管理员审核活动报名")
    @PostMapping("/audit")
    public Result<Boolean> auditActivityRegistration(@RequestBody ActivityAuditDTO auditDTO) {
        try {
            boolean result = activityRegistrationService.auditActivityRegistration(auditDTO);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.fail("审核失败，请稍后重试");
        }
    }

    /**
     * 根据活动ID获取报名列表
     * @param activityId 活动ID
     * @param current 当前页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Operation(summary = "获取活动报名列表")
    @GetMapping("/list/{activityId}")
    public PageResult<ActivityRegistrationResponseDTO> getRegistrationsByActivityId(
            @PathVariable Long activityId,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        try {
            IPage<ActivityRegistrationResponseDTO> result =
                    activityRegistrationService.getRegistrationsByActivityId(activityId, current, size);
            return PageResult.success(result);
        } catch (Exception e) {
            return PageResult.error("获取报名列表失败");
        }
    }

    /**
     * 获取报名列表（支持筛选）
     * @param activityId 活动ID
     * @param status 审核状态
     * @param current 当前页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Operation(summary = "管理员获取报名列表")
    @GetMapping("/list")
    public PageResult<ActivityRegistrationResponseDTO> listRegistrations(
            @RequestParam(required = false) Long activityId,
            @RequestParam(required = false) Byte status,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        try {
            IPage<ActivityRegistrationResponseDTO> result =
                    activityRegistrationService.getRegistrations(activityId, status, current, size);
            return PageResult.success(result);
        } catch (IllegalArgumentException e) {
            return PageResult.error(e.getMessage());
        } catch (Exception e) {
            return PageResult.error("获取报名列表失败");
        }
    }

    /**
     * 获取报名详情
     * @param id 报名记录ID
     * @return 报名详情
     */
    @Operation(summary = "获取报名详情")
    @GetMapping("/detail/{id}")
    public Result<ActivityRegistrationResponseDTO> getRegistrationDetail(@PathVariable Long id) {
        try {
            ActivityRegistrationResponseDTO result = activityRegistrationService.getRegistrationDetail(id);
            if (result == null) {
                return Result.error("报名记录不存在");
            }
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail("查询失败，请稍后重试");
        }
    }
}
